﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using MDL;

namespace DAL
{
    public class DAL_ProductType
    {
        /// <summary>
        /// 获取商品类型信息
        /// </summary>
        /// <returns>商品类型信息</returns>
        public static List<ProductType> GetProductType()
        {
            string sql = "select * from ProductType ";
            List<ProductType> list = new List<ProductType>();
            using (SqlDataReader sdr = DBHelper.白道(sql, null, CommandType.Text))
            {
                while (sdr.Read())
                {
                    ProductType pt = new ProductType();
                    pt.PTID = Convert.ToInt32(sdr[0]);
                    pt.PTName = sdr[1].ToString();
                    list.Add(pt);
                }
            }
            return list;
        }
        /// <summary>
        /// 查询是否已有商品类型名称
        /// </summary>
        /// <param name="name">商品类型名称</param>
        /// <returns>商品类型名称</returns>
        public static List<ProductType> SelectPTName(string name)
        {
            string sql = "select * from ProductType where PTName = @name";
            SqlParameter[] sprs = { new SqlParameter("@name",name)};
            List<ProductType> list = new List<ProductType>();
            using (SqlDataReader dr = DBHelper.白道(sql,sprs,CommandType.Text))
            {
                while (dr.Read())
                {
                    ProductType t = new ProductType();
                    t.PTName = dr[1].ToString();
                    list.Add(t);
                }
            }
            return list;
        }
        /// <summary>
        /// 增加商品类型名称
        /// </summary>
        /// <param name="name">类型名称</param>
        /// <returns>受影响的行数</returns>
        public static int AddPTName(string name)
        {
            string sql = "insert into ProductType values(@name)";
            SqlParameter[] sprs = { new SqlParameter("@name",name)};
            return DBHelper.增删改(sql, sprs, CommandType.Text);
        }
        /// <summary>
        /// 根据商品类型ID删除商品类型
        /// </summary>
        /// <param name="ID">商品类型ID</param>
        /// <returns>受影响的行数</returns>
        public static int DeleteProductType(int ID)
        {
            string sql = "delete from ProductType where PTID = @ID";
            SqlParameter[] sprs = { new SqlParameter("@ID", ID) };
            return DBHelper.增删改(sql, sprs, CommandType.Text);
        }
        /// <summary>
        /// 修改商品类型名称
        /// </summary>
        /// <param name="name">目标商品类型名称</param>
        /// <param name="ID">根据商品类型ID</param>
        /// <returns>受影响的行数</returns>
        public static int UpdateProductType(string name,int ID)
        {
            string sql = "update ProductType set PTName = @name where PTID = @ID";
            SqlParameter[] sprs = { 
                                  new SqlParameter("@name",name),
                                  new SqlParameter("@ID",ID)
                                  };
            return DBHelper.增删改(sql, sprs, CommandType.Text);
        }

    }
}
